package estructuras;
/**
 * CC2003 - Worksheet 3 - Queues in a Bank
 * 
 * For the course CC2003 - Algorithms and Data Structures of UVG (Universidad del Valle de Guatemala, http://uvg.edu.gt)
 * 
 * 
 * SVN Repository of my course projects: http://cc2003.svn.sourceforge.net/viewvc/cc2003/
 * 
 * 
 * Copyright (C) 2009 Carlos Lopez Camey
 * visit me at http://kmels.net or write me at c.lopez@kmels.net
 * 
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/> or write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * @author kmel
 *
 */

public class QueueUsingAnArray<TypeOfTheElements> extends MoculatorQueue<TypeOfTheElements> {
	
	private TypeOfTheElements[] elements;
	
	/**
	 * 
	 */
	public QueueUsingAnArray() {
		this.nelements = 0;
		this.elements = (TypeOfTheElements[])  new Object[this.Size];
	}

	/*
	 * 
	 * @param Element an element to enqueue
	 * @see BankQueue#add(java.lang.Object)
	 */
	public void add(TypeOfTheElements Element) throws FullQueueException {
		//if it's full, you can't enqueue any element
		if (this.isFull())
			throw new FullQueueException("Queue is full");
		
		this.elements[this.nelements++] = Element;
	}

	/*
	 * 
	 * @return the first element of the queue
	 * @see BankQueue#peek()
	 */
	public TypeOfTheElements peek() throws EmptyQueueException {
		if (this.isEmpty())
			throw new EmptyQueueException("Queue is empty, no element to peek");
		
		return this.elements[0];
	}

	public TypeOfTheElements remove() throws EmptyQueueException {
		if (this.isEmpty())
			throw new EmptyQueueException("Queue is empty, no element to peek");
		
		TypeOfTheElements returnValue = this.elements[0];
		
		//go trough the queue and re-assign values 
		for (int i=0; i < this.nelements-1 ; i++)
			this.elements[i] = this.elements[i+1];
		
		this.nelements--;
		return returnValue;
	}

	@Override
	public String toString() {
		String response = "";
		response += "Esta cola tiene " + nelements + " elementos dentro ";
		for (int i=0;i< this.nelements;i++)
			response+= this.elements[i].toString()+" ";
		
		return response;
	}
}
